package com.itcast.rediscache.distributedLock.luaLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * lua分布式锁测试
 * @author zhangtian105
 * @date 2023/5/8
 * @apiNote
 */
@RestController
@RequestMapping(value = "luaLock")
public class LuaLockController {
    @Autowired
    private DistributeLock distributeLock ;



    /**
     * 加锁操作 trylock可以快速返回 lock请求没获取到锁会hang住
     * @return
     */
    @RequestMapping(value = "lockWorker")
    public boolean lockWorker() {
        return distributeLock.trylock("luaLock", "123456789", 10000) ;
    }

    /**
     * 解锁
     * @return
     */
    @RequestMapping(value = "unlockWorker")
    public boolean unlockWorker() {
        return distributeLock.unLock("luaLock", "123456789") ;
    }
}
